package com.animerank.service.impl;

import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.animerank.entity.UserActivity;
import com.animerank.mapper.UserActivityMapper;
import com.animerank.service.IUserActivityService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户活动记录ServiceImpl实现类
 */
@Service
public class UserActivityServiceImpl extends ServiceImpl<UserActivityMapper, UserActivity> implements IUserActivityService {
    
    @Override
    public void recordActivity(Long userId, String activityType, Long animeId, String animeTitle, String ipAddress) {
        UserActivity activity = new UserActivity();
        activity.setUserId(userId);
        activity.setActivityType(activityType);
        activity.setAnimeId(animeId);
        
        // 根据活动类型生成不同的活动内容描述
        String activityContent;
        switch (activityType) {
            case "vote":
                activityContent = "投票给了《" + animeTitle + "》";
                break;
            case "nomination":
                activityContent = "提名了《" + animeTitle + "》";
                break;
            case "favorite":
                activityContent = "收藏了《" + animeTitle + "》";
                break;
            default:
                activityContent = "参与了《" + animeTitle + "》的活动";
        }
        
        activity.setActivityContent(activityContent);
        activity.setActivityTime(LocalDateTime.now());
        activity.setIpAddress(ipAddress);
        
        // 保存活动记录
        this.save(activity);
    }

    
    @Override
    public List<UserActivity> getLatestUserActivities(Long userId, Integer page, Integer pageSize) {
        // 计算偏移量
        int offset = (page - 1) * pageSize;
        return baseMapper.findLatestByUserId(userId, offset, pageSize);
    }
    
    @Override
    public int getUserActivityCount(Long userId) {
        return baseMapper.countByUserId(userId);
    }
}